package com.huluobo.user.view

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import com.google.gson.Gson
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_base.UIState
import com.huluobo.user.R
import com.huluobo.user.UserIntent
import com.huluobo.user.UserViewModel
import com.huluobo.user.entity.ApproveBeanItem
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/2/13.
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ApproveListPage(navHostController: NavHostController, vm: UserViewModel = hiltViewModel()) {
    var list = remember {
        SnapshotStateList<ApproveBeanItem>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.onError -> {
                        Log.e("TagA", "获取请假列表失败:${it.msg}")
                    }

                    UIState.onLoading -> {}
                    is UIState.onSuccess<*> -> {
                        list.clear()
                        list.addAll(it.data as Collection<ApproveBeanItem>)
                    }
                }
            }
        }
        vm.sendIntent(UserIntent.getApproves)
    })

//    val list = vm.getPagingData().collectAsLazyPagingItems()
    var isShow by remember {
        mutableStateOf(false)
    }

    Column(Modifier.padding(10.dp)) {
        Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
            Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "", Modifier.clickable {
                navHostController.popBackStack()
            })
            Text(text = "请假列表")
            Text(
                text = "申请",
                Modifier
                    .background(colorResource(id = R.color.orange), RoundedCornerShape(5.dp))
                    .padding(10.dp, 3.dp)
                    .clickable {
                        navHostController.navigate(RoutePath.ADD_APPROVE)
                    }
            )
        }
        Spacer(modifier = Modifier.height(10.dp))
        Row(
            Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(vertical = 16.dp), horizontalArrangement = Arrangement.SpaceAround
        ) {
            Text(text = "类型")
            Text(text = "开始时间")
            Text(text = "结束时间")
            Text(text = "审批状态")
        }
        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list) {
                Row(
                    Modifier
                        .fillMaxWidth()
                        .background(colorResource(id = R.color.lightgray), RoundedCornerShape(10.dp))
                        .padding(vertical = 16.dp)
                        .combinedClickable(onClick = {
                            val json = Gson().toJson(it)
                            if (it.status == "通过" || it.status == "拒绝") {
                                navHostController.navigate(RoutePath.APPROVE_DETAIL + "/$json")
                            } else {
                                navHostController.navigate(RoutePath.SP + "/$json")
                            }
                        }, onLongClick = {
                            isShow = true
                        }),
                    horizontalArrangement = Arrangement.SpaceAround
                ) {
                    Text(text = "${it.type}")
                    if (!it.startTime.isNullOrEmpty()) {
                        Text(text = "${it.startTime.substring(0, 10)}")
                    }
                    if (!it.endTime.isNullOrEmpty()) {
                        Text(text = "${it.endTime.substring(0, 10)}")
                    }
                    val color =
                        if (it.status == "通过") Color.Green else if (it.status == "拒绝") Color.Red else Color.LightGray
                    Text(
                        text = "${it.status}",
                        Modifier
                            .background(color, RoundedCornerShape(5.dp))
                            .padding(5.dp)
                    )
                }
            }
        })
    }

    if (isShow) {
        AlertDialog(onDismissRequest = { isShow = false }, confirmButton = {
            Text(text = "确定", Modifier.clickable {
                isShow=false

            })
        }, title = { Text(text = "删除提醒") }, text = { Text(text = "是否要删除该条请假信息?") })
    }
}